package com.person.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.person.common.BaseContext;
import com.person.common.CustomException;
import com.person.dto.DishDto;
import com.person.mapper.CategoryMapper;
import com.person.mapper.DishFlavorMapper;
import com.person.mapper.DishMapper;
import com.person.pojo.Category;
import com.person.pojo.Dish;
import com.person.pojo.DishFlavor;
import com.person.pojo.PageBean;
import com.person.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.reactive.TransactionalOperator;

import javax.xml.stream.events.DTD;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String name) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //按姓名，条件查询
        List<DishDto> dishes = dishMapper.inquireDishes(name);


        //使用dto对象，遍历
        for (DishDto dish : dishes) {
            //先查询菜品表中的categroyId，再使用此id再categroy表中查找分类名，使用string字符串接收
            String categoryName =(String) (categoryMapper.inquireCategory(dish.getCategoryId()));
            //将此分类名传入dto对象中，（dto中继承了dish的set get方法，可以对dish中的属性操作）
            dish.setCategoryName(categoryName);
        }

        Page<DishDto> p= (Page<DishDto>) dishes;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Long> ids) {
        //查找是否有正在起售的菜品
        int count = dishMapper.selectStatus(ids);

        if (count>0) {
            throw new CustomException("菜品状态为起售，无法删除");
        }
        dishMapper.delete(ids);
    }

    @Override
    @Transactional   //涉及多张表的操作，需要加入事物控制
    public void addDish(DishDto dto) {
        //雪花算法生成id
        dto.setId(IdUtil.getSnowflakeNextId());
        //设置菜品信息，添加进表dish
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        dto.setCreateUser(BaseContext.getCurrentId());
        dto.setUpdateUser(BaseContext.getCurrentId());
        log.info("添加菜品，设置后{}",dto);
        dishMapper.add(dto);


    }

    @Override
    public DishDto echo(Long id) {
        DishDto dishDto = dishMapper.select(id);

        //使用菜品id查找到口味对应的 dish_id，得到dishflavor的集合对象
        List<DishFlavor> flavor = dishFlavorMapper.select(dishDto.getId());
        dishDto.setFlavors(flavor);


        return dishDto;
    }

    @Override
    public void update(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setUpdateUser(BaseContext.getCurrentId());
        log.info("修改后的保存的菜品信息：{}",dishDto);
        dishMapper.update(dishDto);
    }

    @Override
    public List<DishDto> selectDish(Long id) {
        List<DishDto> dishes = dishMapper.selectDishes(id);

        log.info("套餐id对应的菜品：{}",dishes);
        return dishes;
    }

    @Override
    public void stopStatus(List<Long> ids) {
        dishMapper.updateStopStatus(ids);
    }

    @Override
    public void startStatus(List<Long> ids) {
        dishMapper.updateStartStatus(ids);
    }
}
